﻿using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Interface.SystemSetting;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Service.SystemSetting;
using PhysicalExamination5.WebApi.Utility.JWT;
using System.IdentityModel.Tokens.Jwt;

namespace PhysicalExamination5.WebApi.Controllers.DataMaintenance
{
    /// <summary>
    /// 危害因素维护
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class WeiHaiYinSuWeiHuController : Controller
    {
        private readonly IWeiHaiYinSuWeiHu _IWeiHaiYinSuWeiHu;
        private readonly ICustomJWTService _ICustomJWTService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private static readonly ILog log = LogManager.GetLogger(typeof(IndexController));

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="IWeiHaiYinSuWeiHu"></param>
        /// <param name="ICustomJWTService"></param>
        /// <param name="httpContextAccessor"></param>
        public WeiHaiYinSuWeiHuController(IWeiHaiYinSuWeiHu IWeiHaiYinSuWeiHu, ICustomJWTService ICustomJWTService, IHttpContextAccessor httpContextAccessor)
        {
            _IWeiHaiYinSuWeiHu = IWeiHaiYinSuWeiHu;
            _ICustomJWTService = ICustomJWTService;
            _httpContextAccessor = httpContextAccessor;

            //配置日志ip地址参数信息
            string ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            log4net.GlobalContext.Properties["localIpAddress"] = ipAddress;
        }
        /// <summary>
        /// 查询全部组合项目
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelItemAll()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.GetSelItemAll());
        }
        /// <summary>
        /// 查询危害因素
        /// </summary>
        /// <param name="strZhongLei"></param>
        /// <param name="strName"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelWeiHaiYinSu(string strZhongLei, string? strName)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.GetSelWeiHaiYinSu(strZhongLei,strName));
        }
        /// <summary>
        /// 查询危害因素项目、检查内容、监护依据、国标内容
        /// </summary>
        /// <param name="strWeiHaiYinSuId"></param>
        /// <param name="strGangQi"></param>
        /// <param name="strWeiHaiYinSuGuoBiaoDaiMa"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelWeiHaiYinSuData(string strWeiHaiYinSuId, string strGangQi, string? strWeiHaiYinSuGuoBiaoDaiMa)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.GetSelWeiHaiYinSuData(strWeiHaiYinSuId, strGangQi, strWeiHaiYinSuGuoBiaoDaiMa));
        }
        /// <summary>
        /// 查询国标对应的本地组合项目
        /// </summary>
        /// <param name="strGuoBiaoZuHeXiangMuDaiMa"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelGuoBiaoDuiYinItem(string strGuoBiaoZuHeXiangMuDaiMa)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.GetSelGuoBiaoDuiYinItem(strGuoBiaoZuHeXiangMuDaiMa));
        }
        /// <summary>
        /// 新增危害因素
        /// </summary>
        /// <param name="addWeiHaiYinSu"></param>
        /// <param name="strYuanQuQianZhui"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize] 
        [Route("[action]")]
        public IActionResult PostWeiHaiYinSuAdd(WeiHaiYinSuClass WeiHaiYinSu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IWeiHaiYinSuWeiHu.PostWeiHaiYinSuAdd(WeiHaiYinSu, strYuanQuQianZhui));
        }
        /// <summary>
        /// 修改危害因素
        /// </summary>
        /// <param name="WeiHaiYinSu"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutWeiHaiYinSuUpdate(WeiHaiYinSuClass WeiHaiYinSu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PutWeiHaiYinSuUpdate(WeiHaiYinSu));
        }
        /// <summary>
        /// 删除危害因素
        /// </summary>
        /// <param name="deleteWeiHaiYinSu"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteWeiHaiYinSuDelete(DeleteWeiHaiYinSuClass deleteWeiHaiYinSu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.DeleteWeiHaiYinSuDelete(deleteWeiHaiYinSu));
        }
        /// <summary>
        /// 调整危害因素顺序
        /// </summary>
        /// <param name="weiHaiYinSuShunXu"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutWeiHaiYinSunTiaoZhengShunXu(WeiHaiYinSuShunXuClass weiHaiYinSuShunXu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PutWeiHaiYinSunTiaoZhengShunXu(weiHaiYinSuShunXu));
        }
        /// <summary>
        /// 危害因素另存
        /// </summary>
        /// <param name="weiHaiYinSuLingCun"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostWeiHaiYinSuLingCun(WeiHaiYinSuLingCunClass weiHaiYinSuLingCun)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IWeiHaiYinSuWeiHu.PostWeiHaiYinSuLingCun(weiHaiYinSuLingCun, strYuanQuQianZhui));
        }
        /// <summary>
        /// 危害因素组合项目添加
        /// </summary>
        /// <param name="weiHaiYinSuItemAdd"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostWeiHaiYinSuItemAdd(WeiHaiYinSuItemAddClass weiHaiYinSuItemAdd)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PostWeiHaiYinSuItemAdd(weiHaiYinSuItemAdd));
        }
        /// <summary>
        /// 危害因素组合项目修改
        /// </summary>
        /// <param name="weiHaiYinSuItemData"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutWeiHaiYinSuItemUpdate(WeiHaiYinSuItemDataClass weiHaiYinSuItemData)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PutWeiHaiYinSuItemUpdate(weiHaiYinSuItemData));
        }
        /// <summary>
        /// 危害因素组合项目删除
        /// </summary>
        /// <param name="weiHaiYinSuItemData"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteWeiHaiYinSuItemDelete(WeiHaiYinSuItemDataClass weiHaiYinSuItemData)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.DeleteWeiHaiYinSuItemDelete(weiHaiYinSuItemData));
        }
        /// <summary>
        /// 危害因素组合项目调整顺序
        /// </summary>
        /// <param name="weiHaiYinSuItemShunXu"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutWeiHaiYinSuItemTiaoZhengShunXu(WeiHaiYinSuItemShunXuClass weiHaiYinSuItemShunXu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PutWeiHaiYinSuItemTiaoZhengShunXu(weiHaiYinSuItemShunXu));
        }
        /// <summary>
        /// 危害因素监护依据新增
        /// </summary>
        /// <param name="weiHaiYinSuJianHuYiJuAdd"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostWeiHaiYinSuJianHuYiJuAdd(WeiHaiYinSuJianHuYiJuAddClass weiHaiYinSuJianHuYiJuAdd)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IWeiHaiYinSuWeiHu.PostWeiHaiYinSuJianHuYiJuAdd(weiHaiYinSuJianHuYiJuAdd, strYuanQuQianZhui));
        }
        /// <summary>
        /// 危害因素监护依据修改
        /// </summary>
        /// <param name="weiHaiYinSuJianHuYiJuData"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutWeiHaiYinSuJianHuYiJuUpdate(WeiHaiYinSuJianHuYiJuDataClass weiHaiYinSuJianHuYiJuData)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PutWeiHaiYinSuJianHuYiJuUpdate(weiHaiYinSuJianHuYiJuData));
        }
        /// <summary>
        /// 危害因素监护依据删除
        /// </summary>
        /// <param name="weiHaiYinSuJianHuYiJuData"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteWeiHaiYinSuJianHuYiJuDelete(WeiHaiYinSuJianHuYiJuDataClass weiHaiYinSuJianHuYiJuData)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.DeleteWeiHaiYinSuJianHuYiJuDelete(weiHaiYinSuJianHuYiJuData));
        }
        /// <summary>
        /// 危害因素监护依据调整顺序
        /// </summary>
        /// <param name="weiHaiYinSuJianHuYiJuShunXu"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutWeiHaiYinSuJianHuYiJuTiaoZhengShunXu(WeiHaiYinSuJianHuYiJuShunXuClass weiHaiYinSuJianHuYiJuShunXu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PutWeiHaiYinSuJianHuYiJuTiaoZhengShunXu(weiHaiYinSuJianHuYiJuShunXu));
        }
        /// <summary>
        /// 危害因素统一更换组合项目
        /// </summary>
        /// <param name="weiHaiYinSuTongYiGengHuanItem"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutWeiHuanYinSuTongYiGengHuanItem(WeiHaiYinSuTongYiGengHuanItemClass weiHaiYinSuTongYiGengHuanItem)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PutWeiHuanYinSuTongYiGengHuanItem(weiHaiYinSuTongYiGengHuanItem));
        }
        /// <summary>
        /// 危害因素统一添加组合项目
        /// </summary>
        /// <param name="weiHaiYinSuTongYiItem"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostWeiHaiYinSuTongYiAddItem(WeiHaiYinSuTongYiItemClass weiHaiYinSuTongYiItem)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PostWeiHaiYinSuTongYiAddItem(weiHaiYinSuTongYiItem));
        }
        /// <summary>
        /// 危害因素统一删除组合项目
        /// </summary>
        /// <param name="weiHaiYinSuTongYiItem"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteWeiHaiYinSuTongYiDeleteItem(WeiHaiYinSuTongYiItemClass weiHaiYinSuTongYiItem)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.DeleteWeiHaiYinSuTongYiDeleteItem(weiHaiYinSuTongYiItem));
        }
        /// <summary>
        /// 国标对应本地组合项目添加
        /// </summary>
        /// <param name="weiHaiYinSuDuiYinBenDiItemAdd"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostGuoBiaoDuiYinBenDiItemAdd(WeiHaiYinSuDuiYinBenDiItemAddClass weiHaiYinSuDuiYinBenDiItemAdd)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.PostGuoBiaoDuiYinBenDiItemAdd(weiHaiYinSuDuiYinBenDiItemAdd));
        }
        /// <summary>
        /// 国标对应本地组合项目删除
        /// </summary>
        /// <param name="weiHaiYinSuDuiYinBenDiItemDelete"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteGuoBiaoDuiYinBenDiItemDelete(WeiHaiYinSuDuiYinBenDiItemDeleteClass weiHaiYinSuDuiYinBenDiItemDelete)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.DeleteGuoBiaoDuiYinBenDiItemDelete(weiHaiYinSuDuiYinBenDiItemDelete));
        }
        /// <summary>
        /// 分析危害因素
        /// </summary>
        /// <param name="strWeiHaiYinSuId"></param>
        /// <param name="strGangQi"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetFenXiWeiHaiYinSu(string? strWeiHaiYinSuId, string? strGangQi)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IWeiHaiYinSuWeiHu.GetFenXiWeiHaiYinSu(strWeiHaiYinSuId, strGangQi));
        }
    }
}
